package com.yida.wy.basic.controller.common;

import cn.hutool.core.bean.BeanUtil;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.file.FileTypeUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.wy.common.domain.dto.WyBaseCommunityListDTO;
import com.yida.wy.common.domain.dto.WyBaseOwnerListDTO;
import com.yida.wy.common.domain.request.WyBaseCommunityQueryRequest;
import com.yida.wy.common.domain.request.WyBaseOwnerQueryRequest;
import com.yida.wy.common.domain.vo.WyBaseOwnerExcelVO;
import com.yida.wy.common.service.IWyBaseCommunityService;
import com.yida.wy.common.service.IWyBaseHouseService;
import com.yida.wy.common.service.IWyBaseOwnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 投票楼栋Controller
 *
 * @author lqq
 * @date 2023-03-22
 */
@RestController
@Api(tags = "小区业主信息管理")
public class WyBaseOwnerController extends BaseController {

    @Resource
    private IWyBaseOwnerService baseOwnerService;

    @Resource
    private IWyBaseHouseService baseHouseService;

    @Resource
    private IWyBaseCommunityService baseCommunityService;

    @ApiOperation(value = "查询小区列表", response = WyBaseCommunityListDTO.class)
    @GetMapping("/baseCommunity/list")
    @RequiresPermissions("gwj:basis:community:list")
    public TableDataInfo communityList(WyBaseCommunityQueryRequest request) {
        startPage();
        List<WyBaseCommunityListDTO> list = baseCommunityService.selectCommunityDtoList(request);
        return getDataTable(list);
    }

    @ApiOperation(value = "查询小区业主列表", response = WyBaseOwnerListDTO.class)
    @GetMapping("/baseOwner/list")
    @RequiresPermissions("gwj:basis:owner:list")
    public TableDataInfo ownerList(WyBaseOwnerQueryRequest request) {
        startPage();
        List<WyBaseOwnerListDTO> list = baseHouseService.selectGwjBaseOwnerDtoListByCommunityId(request);
        return getDataTable(list);
    }



    @ApiOperation("导出业主信息列表")
    @RequiresPermissions("gwj:basis:owner:export")
    @Log(title = "导出业主信息列表", businessType = BusinessType.EXPORT, systemName = "gwj")
    @PostMapping("/baseOwner/export/{communityId}")
    public void export(HttpServletResponse response, @PathVariable Long communityId) {
        List<WyBaseOwnerListDTO> list = baseHouseService.selectGwjBaseOwnerDtoListByCommunityId(new WyBaseOwnerQueryRequest() {
            {
                setCommunityId(communityId);
            }
        });
        ExcelUtil<WyBaseOwnerExcelVO> util = new ExcelUtil<>(WyBaseOwnerExcelVO.class);
        util.exportExcel(response, BeanUtil.copyToList(list, WyBaseOwnerExcelVO.class), "投票业主信息数据");
    }

    @ApiOperation("业主信息导入")
    @ApiImplicitParam(name = "file", paramType = "form", value = "上传文件", dataType = "file", required = true)
    @Log(title = "业主信息导入", businessType = BusinessType.IMPORT, systemName = "gwj")
    @RequiresPermissions("gwj:basis:owner:import")
    @PostMapping("/baseOwner/import/{communityId}")
    public AjaxResult importGwjBaseOwner(MultipartFile file, @PathVariable Long communityId) throws Exception {
        if (file.isEmpty()) {
            return AjaxResult.error("导入业主信息文件不能为空");
        }
        if (!checkImportFile(file)) {
            return AjaxResult.error("不是指定的xlsx，xls导入类型文件");
        }
        String message = baseOwnerService.importGwjBaseOwner(file, communityId);
        return AjaxResult.success(message);
    }

    /**
     * 检测导入文件类型是否合规
     *
     * @param file 文件
     * @return /
     */
    private boolean checkImportFile(MultipartFile file) {
        // 检查文件类型
        String type = FileTypeUtils.getExtension(file);
        // 限制的文件类型集合
        return StringUtils.inStringIgnoreCase(type, "xlsx", "xls");
    }
}
